export default function statement(invoice, plays) {
  let totalAmount = 0;
  let volumeCredits = 0;
  let res = `【用户${invoice.customer}的票据单】`;
  const format = new Intl.NumberFormat("en-US", {style: "currency", currency: "USD", minimumFractionDigits: 2}).format
  for (let perf of invoice.performance) {
    const play = plays[perf.playID];
    let thisAmount = 0;
    switch(play.type) {
      case "悲剧":
        thisAmount = 40000;
        if (perf.audience > 30) {
          thisAmount += 1000 * (perf.audience - 30);
        }
        break;
      case "喜剧": 
        thisAmount = 30000;
        if (perf.audience > 20) {
          thisAmount += 10000 + 500 * (perf.audience - 20);
        }
        thisAmount += 300 * perf.audience
        break
      default:
        throw new Error(`未知的类型: ${play.type}`)
    }
    // 新增数量分
    volumeCredits += Math.max(perf.audience - 30, 0);
    if ("喜剧" === play.type) volumeCredits += Math.floor(perf.audience / 5);
    res += ` ${play.name}: ${format(thisAmount/100)} (${perf.audience} 座)`;
    totalAmount += thisAmount;
  }
  res += ` 所欠贷款是 ${format(totalAmount/100)}`;
  res += ` 你赚了 ${volumeCredits} 分`;
  return res;
} 